package cn.sykj.car.service.impl;

import cn.sykj.car.common.Const;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.File;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.FileMapper;
import cn.sykj.car.service.IFileService;
import cn.sykj.car.utils.Utils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author yixu
 * @since 2020-07-13
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

	/**
	 * 删除附件
	 * @param id
	 * @return
	 */
	@Override
	public Boolean deleteFile(Long id) {
		if (Utils.isEmpty(id)){
			throw new ParameterInvalidException();
		}

		// 查询原始记录
		File o_sysFile = this.getById(id);
		if (Utils.isEmpty(o_sysFile)||o_sysFile.getDelFlag().equals(Const.DEL_FLAG_TRUE)){
			throw new CommonException(StatusCode.FILE_INFO_NOT_FOUND.getCode(),StatusCode.FILE_INFO_NOT_FOUND.getMsg());
		}

		boolean ret = this.removeById(id);
		return ret;
	}

	/**
	 * 保存
	 * @param file
	 * @return
	 */
	@Override
	public Boolean saveImage(File file) {
		if (Utils.isEmpty(file)){
			throw new ParameterInvalidException();
		}
		if(Utils.isEmpty(file.getFileCategory())){
			throw new CommonException(StatusCode.FILE_CATEGORY_NOT_FOUND.getCode(),StatusCode.FILE_CATEGORY_NOT_FOUND.getMsg());
		}
		file.setDelFlag(DeleteFlagEnum.FALSE.VALUE);
		boolean ret = this.save(file);
		return ret;
	}
}
